package com.tian.netty.test.charging;

import com.tian.netty.test.TestReqMsgBase;
import com.tian.util.BCDUtil;
import com.tian.util.DateUtils;

/**
 * {@code @description:} 充电数据请求消息
 *
 * @author tianwc 公众号：Java后端技术全栈
 * 在线刷题 1200+java面试题和1000+篇技术文章：<a href="https://woaijava.cc/">博客地址</a>
 * {@code @date:} 2024-05-13 22:40
 * {@code @version:} 1.0
 */
public class ChargingDataReq extends TestReqMsgBase<ChargingDataDto> {
    /**
     * 消息体长度
     */
    private static final int LENGTH = 38;

    /**
     * @param msgType
     */
    public ChargingDataReq(short msgType) {
        super(msgType);
    }


    @Override
    public void format() {
        this.msgObject = new byte[LENGTH];
        int index = 0;
        //0---0x68
        this.msgObject[index++] = MSG_HEAD;
        //消息类型
        this.msgObject[index++] = (byte) msgType;

        //8位---2到10  充电桩编号
        byte[] gunNo = BCDUtil.str2Bcd(responseBody.getGunNo());
        //前面部分补充0
        for (int i = 0; i < 8 - gunNo.length; i++) {
            this.msgObject[index++] = FILLED_VAL;
        }
        for (byte b : gunNo) {
            this.msgObject[index++] = b;
        }
        //8位 [32, 36, 5, 22, 16, 9, 55]
        byte[] chargingStartTime = BCDUtil.str2Bcd(DateUtils.format(responseBody.getChargingStartTime(), DateUtils.FORMAT_NO_SECOND));
        //前面部分补充0
        for (int i = 0; i < 8 - chargingStartTime.length; i++) {
            this.msgObject[index++] = FILLED_VAL;
        }
        for (byte b : chargingStartTime) {
            this.msgObject[index++] = b;
        }
        //8位 [32, 36, 5, 22, 16, 9, 55]
        byte[] chargingTime = BCDUtil.str2Bcd(DateUtils.format(responseBody.getChargingEndTime(), DateUtils.FORMAT_NO_SECOND));
        //前面部分补充0
        for (int i = 0; i < 8 - chargingTime.length; i++) {
            this.msgObject[index++] = FILLED_VAL;
        }
        for (byte b : chargingTime) {
            this.msgObject[index++] = b;
        }
        byte[] chargingCount = BCDUtil.str2Bcd(responseBody.getChargingCount().toString());
        //前面部分补充0
        for (int i = 0; i < 4 - chargingCount.length; i++) {
            this.msgObject[index++] = FILLED_VAL;
        }
        for (byte b : chargingCount) {
            this.msgObject[index++] = b;
        }
        byte[] userId = BCDUtil.str2Bcd(responseBody.getUserId().toString());
        //前面部分补充0
        for (int i = 0; i < 4 - userId.length; i++) {
            this.msgObject[index++] = FILLED_VAL;
        }
        for (byte b : userId) {
            this.msgObject[index++] = b;
        }
        byte[] remainedChargingTime = BCDUtil.str2Bcd(responseBody.getRemainedChargingTime().toString());
        //前面部分补充0
        for (int i = 0; i < 4 - remainedChargingTime.length; i++) {
            this.msgObject[index++] = FILLED_VAL;
        }
        for (byte b : remainedChargingTime) {
            this.msgObject[index++] = b;
        }

    }
}
